package com.ruoyi.answer.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.annotation.Anonymous;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.answer.domain.vo.ExamCompositionVo;
import com.ruoyi.answer.domain.bo.ExamCompositionBo;
import com.ruoyi.answer.service.IExamCompositionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷和试题关联组成
 *
 * @author kun_er
 * @date 2022-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/composition")
@Anonymous
public class ExamCompositionController extends BaseController {

    private final IExamCompositionService iExamCompositionService;

    /**
     * 查询试卷和试题关联组成列表
     */
//    @SaCheckPermission("answer:composition:list")
//    @Anonymous
    @GetMapping("/list")
    public TableDataInfo<ExamCompositionVo> list(ExamCompositionBo bo, PageQuery pageQuery) {
        return iExamCompositionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷和试题关联组成列表
     */
//    @SaCheckPermission("answer:composition:export")
    @Log(title = "试卷和试题关联组成", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamCompositionBo bo, HttpServletResponse response) {
        List<ExamCompositionVo> list = iExamCompositionService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷和试题关联组成", ExamCompositionVo.class, response);
    }

    /**
     * 获取试卷和试题关联组成详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("answer:composition:query")
    @GetMapping("/{id}")
    public R<ExamCompositionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iExamCompositionService.queryById(id));
    }

    /**
     * 新增试卷和试题关联组成
     */
//    @SaCheckPermission("answer:composition:add")
    @Log(title = "试卷和试题关联组成", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Anonymous
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamCompositionBo bo) {
        return toAjax(iExamCompositionService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改试卷和试题关联组成
     */
//    @SaCheckPermission("answer:composition:edit")
    @Log(title = "试卷和试题关联组成", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamCompositionBo bo) {
        return toAjax(iExamCompositionService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除试卷和试题关联组成
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("answer:composition:remove")
    @Log(title = "试卷和试题关联组成", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iExamCompositionService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
